package com.yangcangclub.commons.utils;

import sun.misc.BASE64Encoder;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;

public final class EncryptUtils {
   /**
     * 生成3DES密钥.
     * @param key_byte seed key
     * @throws Exception
     * @return javax.crypto.SecretKey Generated DES key
     */
    public static javax.crypto.SecretKey genDESKey(byte[] key_byte) throws Exception {
    	return new javax.crypto.spec.SecretKeySpec(key_byte, "DESede");
    }

    /**
     * 3DES加密(String).
     * @param key SecretKey
     * @param src byte[]
     * @throws Exception
     * @return byte[]
     */
    public static String desEncrypt(javax.crypto.SecretKey key, String src) throws Exception {
        return new String(desEncrypt(key, src.getBytes()));
    }
    
    /**
     * 3DES 解密(String).
     * @param key SecretKey
     * @param crypt byte[]
     * @throws Exception
     * @return byte[]
     */
    public static String desDecrypt(javax.crypto.SecretKey key, String crypt) throws Exception {
        return new String(desDecrypt(key, crypt.getBytes()));
    }

    /**
     * 3DES加密(byte[]).
     * @param key SecretKey
     * @param src byte[]
     * @throws Exception
     * @return byte[]
     */
    public static byte[] desEncrypt(javax.crypto.SecretKey key, byte[] src) throws Exception {
        javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("DESede");
        cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key);

        return cipher.doFinal(src);
    }
    
    /**
     * 3DES 解密(byte[]).
     * @param key SecretKey
     * @param crypt byte[]
     * @throws Exception
     * @return byte[]
     */
    public static byte[] desDecrypt(javax.crypto.SecretKey key, byte[] crypt) throws Exception {
        javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("DESede");
        cipher.init(javax.crypto.Cipher.DECRYPT_MODE, key);

        return cipher.doFinal(crypt);
    }

    /**
     * MD5 摘要计算(byte[]).
     * @param src byte[]
     * @throws Exception
     * @return byte[] 16 bit digest
     */
    public static byte[] md5Digest(byte[] src) throws Exception {
        MessageDigest alg = MessageDigest.getInstance("MD5");
        // MD5 is 16 bit message digest 

        return alg.digest(src);
    }

    /**
     * MD5 摘要计算(String).
     * @param src String
     * @throws Exception
     * @return String
     */
   public static String md5Digest(String src) throws Exception {
        return new String(md5Digest(src.getBytes()));
    }

    /**
     * BASE64 编码.
     * @param src String inputed string
     * @return String returned string
     */
   public static String base64Encode(String src) {
     //sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
       BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(src.getBytes());
    }
   
   /**
     * BASE64 编码(byte[]).
     * @param src byte[] inputed string
     * @return String returned string
     */
    public static String base64Encode(byte[] src) {
       sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();

        return encoder.encode(src);
    }

    /**
     * BASE64 解码.
     * @param src String inputed string
     * @return String returned string
     */
   public static String base64Decode(String src) {
        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();

        try {
            return new String(decoder.decodeBuffer(src));
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * BASE64 解码(to byte[]).
    * @param src String inputed string
     * @return String returned string
     */
    public static byte[] base64DecodeToBytes(String src) {
        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();

        try {
            return decoder.decodeBuffer(src);
        } catch (Exception ex) {
            return null;
        }
    }
    
    /**
     * 生成随机密码.
     * @param count 密码位数
     * @param letters 是否包含字符
     * @param numbers 是否包含数字
     * @return String password
     */
    public static String getPassword(int count, boolean letters, boolean numbers) {
        return org.apache.commons.lang.RandomStringUtils.random(count, letters, numbers);
    }

    /**
     * 生成字符数字混合的密码.
     * @param count 密码位数
     * @return String password
     */
    public static String getPasswordMix(int count) {
        return getPassword(count, true, true);
    }

    /**
     * 生成纯数字密码.
     * @param count 密码位数
     * @return String password
     */
    public static String getPasswordOfNumber(int count) {
        return getPassword(count, false, true);
    }

    /**
     * 生成纯字符密码.
     * @param count 密码位数
     * @return String password
     */
    public static String getPasswordOfCharacter(int count) {
        return getPassword(count, true, false);
    }
    
    public final static String MD5(String s) {
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};       
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static String toHexString(String s) { 
	    String str=""; 
	    for (int i=0;i<s.length();i++) { 
		    int ch = (int)s.charAt(i); 
		    String s4 = Integer.toHexString(ch); 
		    str = str + s4; 
	    } 
	    return str; 
    } 
    
    public static String URLEncoder(String s) { 
	    if(!StringUtil.isFullEmpty(s)) {
	    	try {
	    		return URLEncoder.encode(s, "UTF-8");
    		} catch (Throwable e) {
    			return null; 
    		}	 
	    }
	    return null;
    } 
    
    public static String URLDecoder(String s) { 
	    if(!StringUtil.isFullEmpty(s)) {
	    	try {
	    		return URLDecoder.decode(s, "UTF-8");
    		} catch (Throwable e) {
    			return null; 
    		}	 
	    }
	    return null;
    } 
    
    public static void main(String[] args) { 
    	
    	System.out.println(EncryptUtils.URLEncoder("http://mp.weixin.qq.com/s?__biz=MTI1MTIyOTYyMQ==&mid=401171459&idx=1&sn=ea5cebc8a82500e147d54f55b8652b0c&3rd=MzA3MDU4NTYzMw==&scene=6#rd"));
    	
    /*
     * 密码生成
     * 	String salt = "41259878035169606411"; EncryptUtils.getPasswordOfNumber(20);
    	String pwd =  EncryptUtils.MD5("likesharing285187035"+"likesharing!@q#f$u%i^b&t*u");
    	 
    	System.out.println("username:admin"+"\r\n 密码：likesharing285187035 \r\n pwd:"+pwd+"\r\n salt:"+salt+"\r\n pswd:"+EncryptUtils.MD5(pwd.toLowerCase()+EncryptUtils.MD5(salt)));
   */
    	
    	//用户表、文章表、广告表
//    	System.out.println(MD5(getPasswordMix(20)+"99"+"likesharing"));
    	
//    	System.out.println(base64Encode("周镇怀周镇怀周镇怀周镇怀周镇怀周镇怀周镇怀"));
//    	System.out.println(base64Decode("5ZGo6ZWH5oCA5ZGo6ZWH5oCA5ZGo6ZWH5oCA5ZGo6ZWH5oCA5ZGo6ZWH5oCA5ZGo6ZWH5oCA5ZGo6ZWH5oCA"));
    	
    	/*String request_token = EncryptUtils.MD5("98FE88417B0B6527A83324A6423F04C0"+"likesharinghumnchou!@#$%^&*()_+OIUHGREF");
    	System.out.println(request_token);*/
  	String salt = "41259878035169606411"; EncryptUtils.getPasswordOfNumber(20);
//   	String pwd =  EncryptUtils.MD5("likesharing285187035"+"likesharing!@q#f$u%i^b&t*u");
    	String pwd =  EncryptUtils.MD5("888888"+"likesharing!@q#f$u%i^b&t*u");
        // 	String pwd =  EncryptUtils.MD5("gtlandplaza"+"likesharing!@q#f$u%i^b&t*u");

    	System.out.println("username:flyger"+"\r\n 密码：likesharing285187035 \r\n pwd:"+pwd+"\r\n salt:"+salt+"\r\n pswd:"+EncryptUtils.MD5(pwd.toLowerCase()+EncryptUtils.MD5(salt)));
   
    }
}
